package com.qianfeng.iot.mqtt.mqtt.handler;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qianfeng.iot.mqtt.service.MqttMessageReceiveService;
import com.qianfeng.iot.sdk.bean.mqtt.MessageHeader;
import com.qianfeng.iot.sdk.bean.mqtt.MessagePayload;
import com.qianfeng.iot.sdk.bean.mqtt.MessageType;
import com.qianfeng.iot.sdk.bean.mqtt.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;

/**
 * Created by Jackiechan on 2022/8/17 20:20
 * 收mqtt消息的service
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Configuration

public class MqttMessageReceiveHandler {


    private MqttMessageReceiveService mqttMessageReceiveService;

    @Autowired
    public void setMqttMessageReceiveService(MqttMessageReceiveService mqttMessageReceiveService) {
        this.mqttMessageReceiveService = mqttMessageReceiveService;
    }

    private ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Bean
    @ServiceActivator(inputChannel = "messageInputChannel")
    public MessageHandler messageHandler() {
        return message -> {
            // System.err.println(message.getPayload());
            //  System.err.println(message.getHeaders());
            //我们约定要求对方发送过来的数据是json格式的数据,并且还得是特定格式的数据
            try {
                MessagePayload messagePayload = objectMapper.readValue(message.getPayload().toString(), MessagePayload.class);
                //必须得有数据
                if (ObjectUtil.isNull(messagePayload)) {
                    messagePayload = new MessagePayload(message.getPayload().toString(), MessageType.DEFAULT);
                } else {
                    //数据没解析到对象上面
                    if (StrUtil.isEmpty(messagePayload.getPayload())) {
                        messagePayload.setPayload(message.getPayload().toString());
                    }

                    if (ObjectUtil.isNull(messagePayload.getMessageType())) {
                        messagePayload.setMessageType(MessageType.DEFAULT);
                    }
                }
                //转换消息的头数据
                MessageHeaders headers = message.getHeaders();
                MessageHeader messageHeader = new MessageHeader(headers);
                //将消息的正文和headler作为参数构建我们自己的消息对象
                MqttMessage mqttMessage = new MqttMessage(messageHeader, messagePayload);
                //将消息交给业务处理
                mqttMessageReceiveService.readValue(mqttMessage);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }

        };
    }
}
